﻿using System;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using Resources;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class KhoiLuongEdit : System.Web.UI.Page
    {
        #region Properties

        private KhoiLuongBiz _khoiLuongBiz;
        private KhoiLuongBiz KhoiLuongManager
        {
            get { return _khoiLuongBiz ?? (_khoiLuongBiz = new KhoiLuongBiz()); }
        }

        private int _khoiLuongId;
        private int KhoiLuongId
        {
            get
            {
                if (_khoiLuongId <= 0)
                    int.TryParse(Request.QueryString["id"], out _khoiLuongId);
                return _khoiLuongId;
            }
        }

        private KhoiLuongHopDongView _khoiLuongHopDongView;
        private KhoiLuongHopDongView CurrentKhoiLuongHopDongView
        {
            get
            {
                if (_khoiLuongHopDongView == null && KhoiLuongId > 0)
                {
                    _khoiLuongHopDongView = KhoiLuongManager.GetById(KhoiLuongId);
                }
                return _khoiLuongHopDongView;
            }
        }
        
        #endregion

        #region Events
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentKhoiLuongHopDongView == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, KhoiLuongId);
                    btnSave.Visible = false;
                    return;
                }

                LoadHopDongData();

                LoadKhoiLuongData();

                // Doi Thi Cong
                var doiThiCongs = DoiThiCongBiz.GetAll();
                doiThiCongs.Insert(0, new DoiThiCong { Id = 0, Ten = string.Empty });
                ddlDoiThiCong.DataTextField = "Ten";
                ddlDoiThiCong.DataValueField = "Id";
                ddlDoiThiCong.DataSource = doiThiCongs;
                ddlDoiThiCong.DataBind();
                ddlDoiThiCong.SelectedValue = string.Format("{0}", CurrentKhoiLuongHopDongView.DoiThiCongId);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            #region Validate inputs

            txtMaTuyen.Text = txtMaTuyen.Text.Trim();
            if (string.IsNullOrEmpty(txtMaTuyen.Text))
            {
                lblErrorMessage.Text = "Chưa nhập mã tuyến";
                return;
            }
            txtDiemDau.Text = txtDiemDau.Text.Trim();
            if (string.IsNullOrEmpty(txtDiemDau.Text))
            {
                lblErrorMessage.Text = "Chưa nhập điểm đầu";
                return;
            }
            txtDiemCuoi.Text = txtDiemCuoi.Text.Trim();
            if (string.IsNullOrEmpty(txtDiemCuoi.Text))
            {
                lblErrorMessage.Text = "Chưa nhập điểm cuối";
                return;
            }

            if (txtDiemDau.Text.Trim().ToLower().Equals(txtDiemCuoi.Text.Trim().ToLower()))
            {
                lblErrorMessage.Text = "Điểm đầu phải khác điểm cuối";
                return;
            }

            #endregion

            #region Collect inputs

            var khoiLuong = new KhoiLuongHopDong
            {
                Id = KhoiLuongId,
                HopDongId = CurrentKhoiLuongHopDongView.HopDongId,
                MaTuyen = txtMaTuyen.Text.Trim(),
                TienDoThiCong = byte.Parse(ddlTienDo.SelectedValue),
                GhiChu = txtNote.Text,
                NhanVatTuA = byte.Parse(ddlNhanVatTuA.SelectedValue),
                DiemDau = txtDiemDau.Text.Trim(),
                DiemCuoi = txtDiemCuoi.Text.Trim(),
                LoaiCap = txtLoaiCap.Text.Trim(),
                DoiThiCongId = int.Parse(ddlDoiThiCong.SelectedValue)
            };

            double chieuDaiTuyen;
            if (double.TryParse(txtChieuDaiTuyen.Text.Trim(), out chieuDaiTuyen))
            {
                khoiLuong.ChieuDaiTuyen = chieuDaiTuyen;
            }

            #endregion

            // save
            KhoiLuongManager.UpdateKhoiLuong(khoiLuong);

            if (Request.Params["src"] == "TienDoThiCong")
            {
                Response.Redirect("TienDoThiCong.aspx");
            }
            else
            {
                Response.Redirect("KhoiLuong.aspx?hopDongId=" + CurrentKhoiLuongHopDongView.HopDongId);
            }
        }

        #endregion

        #region Helpers

        public void LoadHopDongData()
        {
            ltrTenDonVi.Text = CurrentKhoiLuongHopDongView.TenDonVi;
            ltrSoHopDong.Text = CurrentKhoiLuongHopDongView.SoHopDong;
            ltrLoaiDuAn.Text = CurrentKhoiLuongHopDongView.LoaiDuAn;
            ltrTenHopDong.Text = CurrentKhoiLuongHopDongView.TenHopDong;
        }

        public void LoadKhoiLuongData()
        {
            txtMaTuyen.Text = CurrentKhoiLuongHopDongView.MaTuyen;
            txtDiemDau.Text = CurrentKhoiLuongHopDongView.DiemDau;
            txtDiemCuoi.Text = CurrentKhoiLuongHopDongView.DiemCuoi;
            txtChieuDaiTuyen.Text = string.Format("{0:0}", CurrentKhoiLuongHopDongView.ChieuDaiTuyen);
            txtLoaiCap.Text = CurrentKhoiLuongHopDongView.TenLoaiCap;
            txtNote.Text = CurrentKhoiLuongHopDongView.GhiChu;

            ddlTienDo.Items.FindByValue(CurrentKhoiLuongHopDongView.TienDoThiCong.ToString()).Selected = true;
            ddlNhanVatTuA.Items.FindByValue(CurrentKhoiLuongHopDongView.NhanVatTuA.ToString()).Selected = true;
        }

        #endregion
    }
}